# -*- coding: utf_8 -*-
# @Create   : 2021/9/24 9:04
# @Author   : yh
# @Remark   : 巡检记录DB层
from typing import Iterable, List

from mxsoftpy import DB
from mxsoftpy.exception import DBError


class InspectionRecordDB(DB):

    def __except_error(self,fuc, **query):
        try:
            res = fuc(**query)
        except DBError as e:
            if e.err_code == 288:
                return None
            else:
                raise DBError(e.err_code)
        return res

    def select(self,
               videos: Iterable = None,
               tasks: list = None,
               select_data: dict = None,
               count: int = 0
               ) -> List[dict]:
        """
        查询巡检记录
        :param videos: 要查询的所有视频code
        :param tasks: 要查询的所有任务code
        :param select_data: 其他查询参数（test_code、test_value）
        :param count: 查询条数 0为查所有
        """
        record_data = dict()  # 存放所有巡检记录
        id_list = list()
        self.table.open(file='MXSE', table='VideoInspectionRecord')
        if videos:
            temp = set()
            for i in videos:
                res = self.__except_error(self.table.filter, video_code=i, count=count)
                if res:
                    for j in res:
                        record_data[j['_FIELD_AUTOID_']] = j
                        temp.add(j['_FIELD_AUTOID_'])
            id_list.append(temp)

        if tasks:
            temp = set()
            for i in tasks:
                res = self.__except_error(self.table.filter, task_code=i, count=count)
                if res:
                    for j in self.table.filter(task_code=i, count=count):
                        record_data[j['_FIELD_AUTOID_']] = j
                        temp.add(j['_FIELD_AUTOID_'])
            id_list.append(temp)

        if select_data:
            try:
                if select_data['test_code']:
                    temp = set()
                    res = self.__except_error(self.table.filter, test_code=select_data['test_code'], count=count)
                    for j in res:
                        record_data[j['_FIELD_AUTOID_']] = j
                        temp.add(j['_FIELD_AUTOID_'])
                    id_list.append(temp)
            except:
                pass

            try:
                if select_data['test_state']:

                    temp = set()
                    res = self.__except_error(self.table.filter, test_state=select_data['test_state'], count=count)
                    for j in res:
                        record_data[j['_FIELD_AUTOID_']] = j
                        temp.add(j['_FIELD_AUTOID_'])
                    id_list.append(temp)
            except:
                pass

            try:
                if select_data['MXTIME__between']:
                    temp = set()
                    #  查询一个时间范围内的巡检信息
                    res = self.__except_error(self.table.filter,
                                              test_time__between=select_data['MXTIME__between'],
                                              count=count)
                    if res:
                        for j in res:
                            record_data[j['_FIELD_AUTOID_']] = j
                            temp.add(j['_FIELD_AUTOID_'])
                    id_list.append(temp)
            except:
                pass

            try:
                if select_data['test_state__in']:
                    temp = set()
                    #  查询一个时间范围内的巡检信息
                    res = self.__except_error(self.table.filter,
                                              test_time__in=select_data['test_state__in'],
                                              count=count)
                    if res:
                        for j in res:
                            record_data[j['_FIELD_AUTOID_']] = j
                            temp.add(j['_FIELD_AUTOID_'])
                    id_list.append(temp)
            except:
                pass
        if id_list:
            need = id_list[0].intersection(*id_list[1:])  # 所有符合条件的交集
        else:
            need = []

        return [record_data[i] for i in need]



